//
//  SelectedTimeView.m
//  ImGuider
//
//  Created by 王鹏宇 on 2018/1/5.
//  Copyright © 2018年 imguider. All rights reserved.
//

#import "SelectedTimeView.h"
#import "IGCalendarView.h"

@interface SelectedTimeView()<IGCalendarViewDelegate>

@property(nonatomic, strong)UILabel * tipLabel;

@property(nonatomic, strong)IGCalendarView * calendarView;

@property(nonatomic, strong)UILabel * selectCalenderLael;

@property(nonatomic)NSInteger currenIndex;

@property(nonatomic, strong)UIView *timeView;
@end


@implementation SelectedTimeView


- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setup];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setCloseArray:(NSArray *)closeArray {
    _closeArray = closeArray;
    self.calendarView.dayslimit = self.daysLimit.integerValue;
    self.calendarView.closeArray = [NSArray arrayWithArray:closeArray];
}
- (NSString *)currentDateStr {
    
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    // 设置日期格式(为了转换成功)
    fmt.dateFormat = @"yyyy-MM-dd";
    if (self.calendarView.seletedDate) {
        _currentDateStr = [fmt stringFromDate:self.calendarView.seletedDate];
    }
    return _currentDateStr;
}

- (NSDate *)currentDate {
    
    _currentDate = self.calendarView.seletedDate;
    
    return _currentDate;
}

- (void)setIsNeedTime:(BOOL)isNeedTime {
    _isNeedTime = isNeedTime;
    
    self.timeView.hidden = !isNeedTime;
}

- (void)setup {
    
    self.backgroundColor = [UIColor whiteColor];
    self.selectCalenderLael = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, kScreenWidth - 40, 21)];
    self.selectCalenderLael.font = Font(14);
    self.selectCalenderLael.textColor = kSubitleColor;
    self.selectCalenderLael.text = MYLocalizedString(@"TICKET_CHOOSE_DATE", nil);
    [self addSubview:self.selectCalenderLael];
    
    
    CGFloat calendarViewY = CGRectGetMaxY(self.selectCalenderLael.frame);
    self.calendarView = [[IGCalendarView alloc] initWithFrame:CGRectMake(20, calendarViewY, kScreenWidth - 40, (kScreenWidth - 40)/7 * 6 + 60)];
    self.calendarView.seletedDate = self.currentDate;
    self.calendarView.delegate = self;
    [self addSubview:self.calendarView];
    
    NSArray * arr = @[MYLocalizedString(@"TICKET_BOOK_ALLDAY", nil),MYLocalizedString(@"TICKET_BOOK_MORNING", nil),MYLocalizedString(@"TICKET_BOOK_AFTERNOON", nil)];
    
    
     self.timeView = [[UIView alloc] initWithFrame:CGRectMake(20,CGRectGetMaxY(self.calendarView.frame), kScreenWidth - 40, 100)];
    [self addSubview:self.timeView];
    
    self.currenIndex = -1;
    CGFloat Y = 10;
    CGFloat X = 0;
    CGFloat space = 10;
    
   
    for (int i = 0; i < arr.count; i++) {
        
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(X, Y,(kScreenWidth - 40)/7, 20);
        [btn setImage:[UIImage imageNamed:@"button_tick_nor"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"button_tick_sel"] forState:UIControlStateSelected];
        btn.tag = 300 + i;
        [btn addTarget:self action:@selector(selectTapAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.timeView addSubview:btn];
        
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake((kScreenWidth - 40)/7, Y, kScreenWidth - 40 - (kScreenWidth - 40)/7, 20)];
        [self.timeView addSubview:label];
        label.textColor = kContentColor;
        label.font = Font(14);
        label.text = arr[i];
        
        Y =Y + 20 + space;
    }
    
//    self.tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, Y, kScreenWidth - 40, 50)];
//    self.tipLabel.font = Font(14);
//    self.tipLabel.numberOfLines = 3;
//    self.tipLabel.textColor = kContentColor;
//    self.tipLabel.text = @"此产品需二次确认，商家将在18个工作小时内（工作日9:30-18:30）核实是否有位。";
//    [self addSubview:_tipLabel];
}

- (void)selectTapAction:(UIButton *)sender {
    
    NSInteger index = sender.tag - 300;
    
    if (index != self.currenIndex) {
        sender.selected = YES;
        
        UIButton * btn = [self viewWithTag:self.currenIndex + 300];
        btn.selected = NO;
        self.currenIndex = index;
        
        [self tapActionCompelete];
    }
    
}


#pragma mark   -IGCalendarViewDelegate

- (void)selectTheDate:(NSDate *)date {
    
    [self tapActionCompelete];
}


- (void)tapActionCompelete {
    
    if (_delegate && [_delegate respondsToSelector:@selector(selectTheDate:Time:isNeedTime:)]) {
        NSString * str = @"";
        
        if (self.currenIndex == 1) {
            str = @"am";
        }else if(self.currenIndex == 2){
            str = @"pm";
        }else if(self.currenIndex == 0){
            str = @"apm";
        }
        [_delegate selectTheDate:self.currentDate Time:str isNeedTime:self.isNeedTime];
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
